package com.probox.common.entity.service.platfom.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.utils.DateUtils;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.entity.domain.platfom.PlatformStoreGroup;
import com.probox.common.entity.mapper.platfom.PlatformStoreGroupMapper;
import com.probox.common.entity.service.platfom.IPlatformStoreGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * 门店分组管理Service业务层处理
 *
 * @author usoft
 * @date 2022-09-17
 */
@Service
@Transactional
public class PlatformStoreGroupServiceImpl extends ServiceImpl<PlatformStoreGroupMapper, PlatformStoreGroup> implements IPlatformStoreGroupService {
    @Autowired
    private PlatformStoreGroupMapper platformStoreGroupMapper;


    /**
     * 查询门店分组管理
     *
     * @param groupId 门店分组管理ID
     * @return 门店分组管理
     */
    @Override
    public PlatformStoreGroup selectPlatformStoreGroupById(Long groupId) {
        return this.getById(groupId);
    }

    /**
     * 查询门店分组管理列表
     *
     * @param platformStoreGroup 门店分组管理
     * @return 门店分组管理
     */
    @Override
    public List<PlatformStoreGroup> queryList(PlatformStoreGroup platformStoreGroup) {
        QueryWrapper<PlatformStoreGroup> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(platformStoreGroup.getGroupId())) && platformStoreGroup.getGroupId() != null) {
            queryWrapper.eq("group_id", platformStoreGroup.getGroupId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformStoreGroup.getGroupName())) && platformStoreGroup.getGroupName() != null) {
            queryWrapper.like("group_name", platformStoreGroup.getGroupName());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformStoreGroup.getDataStatus())) && platformStoreGroup.getDataStatus() != null) {
            queryWrapper.eq("data_status", platformStoreGroup.getDataStatus());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformStoreGroup.getCreateUser())) && platformStoreGroup.getCreateUser() != null) {
            queryWrapper.eq("create_user", platformStoreGroup.getCreateUser());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformStoreGroup.getCreateName())) && platformStoreGroup.getCreateName() != null) {
            queryWrapper.eq("create_name", platformStoreGroup.getCreateName());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformStoreGroup.getUpdateUser())) && platformStoreGroup.getUpdateUser() != null) {
            queryWrapper.eq("update_user", platformStoreGroup.getUpdateUser());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformStoreGroup.getUpdateName())) && platformStoreGroup.getUpdateName() != null) {
            queryWrapper.eq("update_name", platformStoreGroup.getUpdateName());
        }
        queryWrapper.ne("data_status", DataStatus.delete.getKey());
        if (StringUtils.isNotEmpty(String.valueOf(platformStoreGroup.getStoreIds())) && platformStoreGroup.getStoreIds() != null) {
            queryWrapper.last(" and find_in_set("+platformStoreGroup.getStoreIds()+",store_ids) ");
        }
        List<PlatformStoreGroup> list = platformStoreGroupMapper.selectList(queryWrapper);
        PlatformStoreGroup vo = null;
        Iterator<PlatformStoreGroup> it = list.iterator();
        while (it.hasNext()) {
            vo = it.next();
            vo.setStoreNames(platformStoreGroupMapper.getStoreNames(vo.getStoreIds()));
        }
        return list;
    }

    @Override
    public List<PlatformStoreGroup> groupList(PlatformStoreGroup platformStoreGroup) {
        QueryWrapper<PlatformStoreGroup> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(platformStoreGroup.getGroupId())) && platformStoreGroup.getGroupId() != null) {
            queryWrapper.eq("group_id", platformStoreGroup.getGroupId());
        }
        queryWrapper.eq("data_status", DataStatus.valid.getKey());
        List<PlatformStoreGroup> list = platformStoreGroupMapper.selectList(queryWrapper);
        return list;
    }


    @Override
    public Integer queryCount(PlatformStoreGroup platformStoreGroup) {
        return null;
    }

    /**
     * 新增门店分组管理
     *
     * @param platformStoreGroup 门店分组管理
     * @return 结果
     */
    @Override
    public boolean insertPlatformStoreGroup(PlatformStoreGroup platformStoreGroup) {

        platformStoreGroup.setCreateUser(SecurityUtils.getUserId());
        platformStoreGroup.setCreateName(SecurityUtils.getUsername());
        platformStoreGroup.setCreateTime(DateUtils.getNowDate());

        return this.save(platformStoreGroup);

    }

    /**
     * 修改门店分组管理
     *
     * @param platformStoreGroup 门店分组管理
     * @return 结果
     */
    @Override
    public boolean updatePlatformStoreGroup(PlatformStoreGroup platformStoreGroup) {

        platformStoreGroup.setUpdateTime(DateUtils.getNowDate());
        platformStoreGroup.setUpdateName(SecurityUtils.getUsername());
        platformStoreGroup.setUpdateUser(SecurityUtils.getUserId());
        return this.updateById(platformStoreGroup);
    }

    /**
     * 批量删除门店分组管理
     *
     * @param groupIds 需要删除的门店分组管理ID
     * @return 结果
     */
    @Override
    public boolean deletePlatformStoreGroupByIds(Long[] groupIds) {
        return this.removeByIds(Arrays.asList(groupIds));
    }


}
